package org.next.tesigiulia.views;

import com.google.common.eventbus.Subscribe;
import gov.nasa.worldwind.avlist.AVList;
import gov.nasa.worldwind.avlist.AVListImpl;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.AnnotationLayer;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.GlobeAnnotation;
import gov.nasa.worldwind.render.ShapeAttributes;
import gov.nasa.worldwind.render.SurfaceCircle;
import gov.nasa.worldwind.symbology.SymbologyConstants;
import gov.nasa.worldwind.symbology.TacticalSymbol;
import gov.nasa.worldwind.symbology.milstd2525.MilStd2525TacticalSymbol;
import gov.nasa.worldwind.util.StatusBar;
import org.next.tesigiulia.controllers.ITwitterWWJController;
import org.next.tesigiulia.models.Area;
import org.next.tesigiulia.models.AreasModel;
import org.next.tesigiulia.models.TwitterWWJModel;
import org.next.tesigiulia.utils.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;


public class TwitterWWJView extends JFrame {

    private WorldWindowGLCanvas glCanvas;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu helpMenu;
    private JMenuItem mnuiSetting;
    private JMenuItem mnuExit;
    private JMenuItem mnuiAbout;
    private StatusBar statusBar;
    private ITwitterWWJController controller;
    private AnnotationLayer annotantionLayer;
    private RenderableLayer renderableLayer;
    private Area area;
    private RenderableLayer vesselsLayer;


    public void setController(ITwitterWWJController controller) {
        this.controller = controller;
    }

    public TwitterWWJView(WorldWindowGLCanvas canvas) {

        this.glCanvas = canvas;

        this.setLayout(new BorderLayout());
        this.setTitle(MyConstant.TITOLOFINESTRAWWJ);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);

        this.getContentPane().add((this.glCanvas), BorderLayout.CENTER);

        this.add(getMnuBar(), BorderLayout.NORTH);

        getMnuBar().add(getMnuFile());
        getMnuBar().add(getMnuHelp());

        getMnuFile().add(getMnuiAreas());
        getMnuFile().add(getMnuiExit());
        getMnuHelp().add(getMnuiAbout());

        getMnuBar().add(getMnuFile());
        getMnuBar().add(getMnuHelp());

        this.add(getStatusBar(), BorderLayout.SOUTH);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        MyEventBus.getInstance().getEventBus().register(this);

        canvas.getInputHandler().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MyEventBus.getInstance().getEventBus().post(glCanvas.getCurrentPosition());
            }
        });

        /* AnnotationLayer annotationLayer=new AnnotationLayer();
        */

        //  insertBeforeCompass(getWwd(),annotationLayer);


        //insertBeforeCompass(getWwd(),renderableLayer);
    }

    private JMenuBar getMnuBar() {
        if (menuBar == null) {
            menuBar = new JMenuBar();
            menuBar.setName(MyConstant.MENU_BAR);
            menuBar.setVisible(true);
        }
        return menuBar;

    }

    private JMenu getMnuFile() {
        if (fileMenu == null) {
            fileMenu = new JMenu(MyConstant.FILE);
            fileMenu.setName(MyConstant.LBL_MENU_FILE);
            fileMenu.setVisible(true);
        }
        return fileMenu;
    }

    private JMenu getMnuHelp() {
        if (helpMenu == null) {
            helpMenu = new JMenu(MyConstant.HELP);
            helpMenu.setName(MyConstant.LBL_MENU_HELP);
            helpMenu.setVisible(true);
        }
        return helpMenu;

    }

    private Component getStatusBar() {

        if (statusBar == null) {
            statusBar = new StatusBar();
            statusBar.setName(MyConstant.STATUS_BAR);
            statusBar.setVisible(true);
        }
        return statusBar;
    }


    private JMenuItem getMnuiAbout() {
        if (mnuiAbout == null) {
            mnuiAbout = new JMenuItem(MyConstant.ABOUT);
            mnuiAbout.setName(MyConstant.LBL_ABOUT);

            mnuiAbout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {

                    JOptionPane.showMessageDialog(null, "Twitter WWJ \n" +
                            "Version 0.1 \n", "About", JOptionPane.INFORMATION_MESSAGE);
                }
            });
        }

        return mnuiAbout;
    }

    private JMenuItem getMnuiExit() {
        if (mnuExit == null) {
            mnuExit = new JMenuItem(MyConstant.EXIT);
            mnuExit.setName(MyConstant.LBL_EXIT);
            mnuExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    int n = JOptionPane.showConfirmDialog(null, "Confirm exit?", "Exit", JOptionPane.YES_NO_OPTION);
                    if (n == JOptionPane.YES_OPTION) {
                        System.exit(0);
                    } else if (n == JOptionPane.NO_OPTION) {
                    }
                }
            });

        }
        return mnuExit;
    }

    private JMenuItem getMnuiAreas() {
        if (mnuiSetting == null) {
            mnuiSetting = new JMenuItem(MyConstant.AREAS);
            mnuiSetting.setName(MyConstant.LBL_AREAS);
            mnuiSetting.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    controller.itemAreasClicked();

                }
            });
        }
        return mnuiSetting;
    }

    public void setAnnotantionLayer(AnnotationLayer annotantionLayer) {
        this.annotantionLayer = annotantionLayer;
        this.glCanvas.getModel().getLayers().add(annotantionLayer);

    }

    private AnnotationLayer getAnnotantionLayer() {

        return this.annotantionLayer;

        /*GlobeAnnotation globeAnnotation=new GlobeAnnotation("Ciao", Position.fromDegrees(42, 12));
        annotationLayer.addAnnotation(globeAnnotation);*/
    }

    public void setRenderableLayer(RenderableLayer renderableLayer) {
        this.renderableLayer = renderableLayer;
        this.glCanvas.getModel().getLayers().add(renderableLayer);
    }

    private RenderableLayer getRenderableLayer() {
        return this.renderableLayer;
    }

    @Subscribe
    public void handleModel(TwitterWWJModel model) {
        this.setVisible(model.isViewVisible());
    }


    @Subscribe
    public void handleAreas(ArrayList<Area> list) {
        this.getRenderableLayer().removeAllRenderables();
        this.getAnnotantionLayer().removeAllAnnotations();

        for (int i = 0; i < list.size(); i++) {
            area = list.get(i);
            drawCircle();
        }

    }

    @Subscribe
    public void handleModifyAreas(AreasModel areasModel) {
        this.getRenderableLayer().removeAllRenderables();

        for (int i = 0; i < areasModel.size(); i++) {
            area = areasModel.get(i);
            drawCircle();
        }
        this.glCanvas.redraw();

    }


    public void drawCircle() {
        Double lat = area.getLatitudine();
        Double lon = area.getLongitudine();
        Integer radius = area.getRaggio();

        LatLon position = new LatLon(Angle.fromDegrees(lat), Angle.fromDegrees(lon));
        SurfaceCircle circle = new SurfaceCircle(position, radius * 1000);
        ShapeAttributes attr = new BasicShapeAttributes();
        attr.setInteriorOpacity(0.5);
        circle.setVisible(true);
        circle.setAttributes(attr);
        this.getRenderableLayer().isEnabled();
        this.getRenderableLayer().addRenderable(circle);
    }

    @Subscribe
    public void handleAnnotations(TwitterEvent twitterEvent) {

        String info = twitterEvent.getStatus().getText();
        Double lat = twitterEvent.getStatus().getGeoLocation().getLatitude();
        Double lon = twitterEvent.getStatus().getGeoLocation().getLongitude();

        Position position = new Position(Angle.fromDegrees(lat), Angle.fromDegrees(lon), 0);

        GlobeAnnotation globeAnnotation = new GlobeAnnotation(info, position);
        getAnnotantionLayer().addAnnotation(globeAnnotation);


    }

    @Subscribe
    public void handleVesselsList(MyVesselsList vesselList) {
        this.getVesselsLayer().removeAllRenderables();
        for (Vessel vessel : vesselList.getList()) {
            String mercantile = "SUSPXM---------";
            AVList modifiers = new AVListImpl();

            modifiers.setValue(SymbologyConstants.DIRECTION_OF_MOVEMENT, Angle.fromDegrees(vessel.getAngle()));
            modifiers.setValue(SymbologyConstants.UNIQUE_DESIGNATION, vessel.getName() + " " + vessel.getMmsi());
            TacticalSymbol symbol = new MilStd2525TacticalSymbol(mercantile, Position.fromDegrees(vessel.getLat(), vessel.getLon()), modifiers);
            this.getVesselsLayer().addRenderable(symbol);
        }
        glCanvas.redraw();
    }


    public void setVesselsLayer(RenderableLayer vesselsLayer) {
        this.vesselsLayer = vesselsLayer;
        glCanvas.getModel().getLayers().add(vesselsLayer);
    }

    private RenderableLayer getVesselsLayer() {
        return vesselsLayer;
    }
}


